#include "stdafx.h"

#include <cctype>			// for std::tolower

#include "..\..\Utilities\String.h"

#include "DevelopmentResourceFile.h"

DevelopmentResourceFile::DevelopmentResourceFile(const std::wstring resFileName)
{ 
	TCHAR dir[MAX_PATH];

	GetCurrentDirectory(MAX_PATH, dir);

	_assetsDir = dir;

	int lastSlash = _assetsDir.find_last_of(L"\\");

	_assetsDir = _assetsDir.substr(0, lastSlash);

	lastSlash = _assetsDir.find_last_of(L"\\");

	_assetsDir = _assetsDir.substr(0, lastSlash);

	_assetsDir +=  L"\\Assets\\";
}

int DevelopmentResourceFile::Find(const std::string& name)
{
	std::string lowerCase = name;

	std::transform(lowerCase.begin(), lowerCase.end(), lowerCase.begin(), (int(*)(int)) std::tolower);

	ZipContentsMap::const_iterator i = _directoryContentsMap.find(lowerCase);

	if (i==_directoryContentsMap.end())
	{
		return -1;
	}

	return i->second;
}

bool DevelopmentResourceFile::Open()
{
	ReadAssetsDirectory(L"*");

	return true;
}

int DevelopmentResourceFile::GetRawResourceSize(const Resource& r)
{
	int num = Find(r.Name.c_str());

	if (num == -1)
		return -1;

	return _assetFileInfo[num].nFileSizeLow;
}

int DevelopmentResourceFile::GetRawResource(
	const Resource &r, 
	char *buffer)
{
	int num = Find(r.Name.c_str());

	if (num == -1)
		return -1;

	std::string fullFileSpec = ws2s(_assetsDir) + r.Name.c_str(); 

	FILE *f = fopen(fullFileSpec.c_str(), "rb");

	size_t bytes = fread(buffer, 1, _assetFileInfo[num].nFileSizeLow, f);

	fclose(f);

	return bytes;
}

int DevelopmentResourceFile::GetNumResources() const 
{
	return _assetFileInfo.size();
}

std::string DevelopmentResourceFile::GetResourceName(int num) const 
{
	std::wstring wideName = _assetFileInfo[num].cFileName;

	return ws2s(wideName);
}

void DevelopmentResourceFile::ReadAssetsDirectory(std::wstring fileSpec)
{
      HANDLE fileHandle;

      WIN32_FIND_DATA findData;
 
      // get first file
	  std::wstring pathSpec = _assetsDir + fileSpec;

      fileHandle = FindFirstFile( pathSpec.c_str(), &findData );

      if( fileHandle != INVALID_HANDLE_VALUE )
      {
           // loop on all remeaining entries in dir
            while( FindNextFile( fileHandle,&findData ) )
            {
				if (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
					continue;

				std::wstring fileName = findData.cFileName; 
				if( findData.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY )
				{
					if (fileName != L".." && fileName != L".")
					{
						fileName = fileSpec.substr(0, fileSpec.length()-1) + fileName + L"\\*";
						ReadAssetsDirectory(fileName);
					}
				}
				else
				{
					fileName = fileSpec.substr(0, fileSpec.length()-1) + fileName;
					std::wstring lower = fileName;
					std::transform(lower.begin(), lower.end(), lower.begin(), (int(*)(int)) std::tolower);
					wcscpy_s(&findData.cFileName[0], MAX_PATH, lower.c_str());
					_directoryContentsMap[ws2s(lower)] = _assetFileInfo.size();
					_assetFileInfo.push_back(findData);
				} 
            }
      } 

      FindClose( fileHandle );
}